package cn.neusoft.customer.controller;

import cn.neusoft.customer.common.ResponseEntity;
import cn.neusoft.customer.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.minidev.json.JSONObject;
import org.example.entity.Customer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/customer")
@CrossOrigin
public class CustomerController {

    @Resource
    private CustomerService customerService;

    //可以根据电话号码、身份证、姓名查询
    @PostMapping("/getByCondition")
    public ResponseEntity<Object> getByCondition(@RequestBody JSONObject params) {
        ResponseEntity<Object> httpResponse = new ResponseEntity<>();
        try {
            int currentPage = (int) params.get("currentPage");
            int pageSize = (int) params.get("pageSize");

            // 创建一个QueryWrapper对象
            QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
            String customerName = params.getAsString("customerName");
            String cardId = params.getAsString("cardId");
            String phone = params.getAsString("phone");
            if (customerName != null) {
                customerQueryWrapper.like("customer_name", customerName);
            }
            if (cardId != null) {
                customerQueryWrapper.like("card_id", cardId);
            }
            if (phone != null) {
                customerQueryWrapper.like("phone", phone);
            }
            customerQueryWrapper.last("LIMIT " + (currentPage - 1) * pageSize + "," + pageSize);
            List<Customer> list = customerService.list(customerQueryWrapper);
            int size = customerService.count();
            httpResponse.setData(list);
            Map<String, Integer> map = new HashMap<>();
            map.put("totalNum", size);
            httpResponse.setMap(map);
            httpResponse.setCode(1);
        } catch (Exception e) {
            return ResponseEntity.error("系统错误：" + e.getMessage());
        }
        return httpResponse;
    }

    //信息录入需要：客户编号、客户姓名、身份证号、工作单位、座机、客户电话、联系地址、邮编、Email
    @PostMapping("/add")
    public ResponseEntity<String> customerAdd(@RequestBody Customer customer) {
        try {
            customerService.save(customer);
            return ResponseEntity.success("添加成功");
        } catch (Exception e) {
            return ResponseEntity.error("系统错误：" + e.getMessage());
        }
    }

    @PostMapping("/info/{id}")
    public ResponseEntity<Customer> customerInfo(@PathVariable String id) {
        ResponseEntity<Customer> httpResponse = new ResponseEntity<>();
        try {
            Customer customer = customerService.getById(id);
            if (customer != null) {
                httpResponse.setData(customer);
                httpResponse.setCode(1);
            } else {
                httpResponse.setCode(-1);
            }
        } catch (Exception e) {
            return ResponseEntity.error("系统错误：" + e.getMessage());
        }
        return httpResponse;
    }

    @PostMapping("/update")
    public ResponseEntity<String> customerUpdate(@RequestBody Customer customer) {
        try {
            customerService.updateById(customer);
            return ResponseEntity.success("修改成功");
        } catch (Exception e) {
            return ResponseEntity.error("系统错误：" + e.getMessage());
        }
    }

    @PostMapping("/remove")
    public ResponseEntity<String> customerRemove(@RequestBody JSONObject params) {
        try {
            if (customerService.removeById(params.getAsString("customer_id"))) {
                return ResponseEntity.success("删除成功");
            } else {
                return ResponseEntity.error("删除失败");
            }
        } catch (Exception e) {
            return ResponseEntity.error("系统错误：" + e.getMessage());
        }
    }

    @PostMapping("/getNames")
    public ResponseEntity<List<String>> getNames() {
        ResponseEntity<List<String>> httpResponse = new ResponseEntity<>();
        try {
            QueryWrapper<Customer> customerQueryWrapper = new QueryWrapper<>();
            customerQueryWrapper.select("customer_name");
            List<String> list = customerService.list().stream()
                    .map(Customer::getCustomerName)
                    .collect(Collectors.toList());
            httpResponse.setData(list);
            httpResponse.setCode(1);
        } catch (Exception e) {
            return ResponseEntity.error("系统错误：" + e.getMessage());
        }
        return httpResponse;
    }


}
